using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CodeWork.DAL.BDHomePage;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_SiteUserEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        SiteUser siteUser;

        if (Int32.TryParse(Request["id"], out id))
            siteUser = SiteUser.Load(id);
        else
            siteUser = null;

        if (siteUser != null)
        {
            if (!string.IsNullOrEmpty(siteUser.UserLoginUserName))
				tbxUserLoginUserName.Text = siteUser.UserLoginUserName;
			if (!string.IsNullOrEmpty(siteUser.UserLoginPassword))
				tbxUserLoginPassword.Text = siteUser.UserLoginPassword;
			if (!string.IsNullOrEmpty(siteUser.UserEmail))
				tbxUserEmail.Text = siteUser.UserEmail;
			if (!string.IsNullOrEmpty(siteUser.DeliciousUserName))
				tbxDeliciousUserName.Text = siteUser.DeliciousUserName;
			if (!string.IsNullOrEmpty(siteUser.DeliciousPassword))
				tbxDeliciousPassword.Text = siteUser.DeliciousPassword;
			tbxUserRoleId.Text = siteUser.UserRoleId.ToString();
			if (siteUser.LastVisit != SqlDateTime.MinValue)
				tbxLastVisit.Text = siteUser.LastVisit.ToShortDateString();

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string userLoginUserName = Security.BindStringInput(tbxUserLoginUserName.Text, _securityLevel);
		string userLoginPassword = Security.BindStringInput(tbxUserLoginPassword.Text, _securityLevel);
		string userEmail = Security.BindStringInput(tbxUserEmail.Text, _securityLevel);
		string deliciousUserName = Security.BindStringInput(tbxDeliciousUserName.Text, _securityLevel);
		string deliciousPassword = Security.BindStringInput(tbxDeliciousPassword.Text, _securityLevel);
		int userRoleId = Security.BindIntegerInput(tbxUserRoleId.Text, _securityLevel);
		DateTime lastVisit = Security.BindDateTimeInput(tbxLastVisit.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (SiteUser.Save(
				id, 
				userLoginUserName,
				userLoginPassword,
				userEmail,
				deliciousUserName,
				deliciousPassword,
				userRoleId,
				lastVisit
                ))
                    Response.Redirect("siteuserlist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            SiteUser siteUser = SiteUser.Create(
				userLoginUserName,
				userLoginPassword,
				userEmail,
				deliciousUserName,
				deliciousPassword,
				userRoleId,
				lastVisit
                );

            if (siteUser != null)
                Response.Redirect("siteuserlist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("siteuserlist.aspx");
    }
}
